﻿@using KPIS.GERP.Models.ViewModel
@model IEnumerable<KPIS.GERP.Models.ICM.PurchaseOrderInstallment>
@{
    //Layout = "~/Views/Shared/_EmptyLayout.cshtml";
	ViewBag.Title = "รายการที่ยังไม่ได้ตรวจรับ";
	ViewBag.Descr = "แสดงข้อมูลรายการแบ่งงวดการส่งมอบที่ยังไม่ถูกทำการตรวจรับ";
     //int intPoId = ViewBag.PoId;
    var db = new GERPContext();
    var allInstall = db.PurchaseOrderInstallments.OrderBy(o => o.PoId).ThenBy(o => o.InstallNumber).ToList();
    var allInstallItem = db.PurchaseOrderInstallmentItems.OrderBy(o => o.PoInstallId).ToList();
    var currentYear = (DateTime.Now.Month < 10 ? DateTime.Now : DateTime.Now.AddYears(1))
        .ToString("yyyy", new System.Globalization.CultureInfo("th-TH"));
    var yearParam = Request["bgyear"];
    var yearSelected = string.IsNullOrWhiteSpace(yearParam) ? currentYear : yearParam;
}

@using (Html.BeginForm("DeleteAll", "DeliveryNote", FormMethod.Post, new { id = "form" }))
{

    <div class="block-button-menu-above">
        <table width="99%">
            <tr>
                <td>
                    <a class="btn button-standard-width btn-small btn-gray" data-rel="tooltip" href="@Url.Action("Index","DeliveryNote")" 
                       role="button"><i class="icon-reply bigger-120"></i>&nbsp;&nbsp;กลับไปหน้าแสดงข้อมูลการตรวจรับทั้งหมด</a>
                </td>
                <td class="control-label" align="right">เลือกปีงบประมาณ</td>
                @*<td>
                        @Html.DropDownList("bgyear",
            new[] { new { Text = "ทั้งหมด", Value = "ทั้งหมด" } }
            .Concat(Enumerable.Range(2557, 2580).Select(o => new { Text = o.ToString(), Value = o.ToString() }))
            .Select(o => new SelectListItem()
        {
            Text = o.Text + (o.Value == yearSelected ? "*":""),
            Value = o.Value,
            Selected = o.Value == yearSelected
        }),
            new { style = "margin-top:10px;" })
                </td>*@
            </tr>
        </table>


</div>

       @Html.AntiForgeryToken()

        <div class="row-fluid">    


        @Html.ValidationSummary(true, "", new { @class = "alert alert-error" })

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
					@ViewBag.Title
                </div>
				@Html.Partial("_ExportButton")
            </div>
        </div>
  
    
    <table id="table"  class ="table table-striped table-bordered table-hover"  aria-describedby="table_info">
        <thead >
             <tr>
                 <th class ="center"> @*<label ><input type ="checkbox" /><span class ="lbl"></span></label>*@   </th>
                 <th class="center">รายการ</th>
                 <th style="width:60px">งวด</th>
                 <th class="hidden-480" style="width: 110px">วันที่</th>
                 <th style="width: 100px">ประเภท</th>
                 <th class="hidden-480" style="width: 120px">ผู้ขาย/รับจ้าง</th>
                 <th class="hidden-480" style="width: 80px">สถานะ</th>
                 <th class="center" style="width:80px;"><!-- Empty head column for export buttons --></th>
	        </tr>					
		</thead>       
          
    <tbody>
        @{
            int i = 1;
            var models = Model;
            //if (yearSelected != "ทั้งหมด")
            //{
            //    models = models.Where(o => o.PurchaseOrder != null &&
            //        o.PurchaseOrder.RequireDate != null &&
            //        (o.PurchaseOrder.RequireDate.Value.Month < 10 ?
            //        o.PurchaseOrder.RequireDate.Value :
            //        o.PurchaseOrder.RequireDate.Value.AddYears(1))
            //        .ToString("yyyy", new System.Globalization.CultureInfo("th-TH"))  
            //        == yearSelected);
            //} 
        }
        @foreach (var m in models.OrderByDescending(o => o.PurchaseOrder.RequireDate))
        { 
            <tr>
                <td class="center">
                    @(i++)
                    @*<label>
                        <input type ="checkbox" name="deleteChk" value ="@m.PoId" />
                        <span class="lbl"></span>

                    </label>*@
                </td>

                <td>
                   
                    @Html.DisplayFor(model=>m.PurchaseOrder.Name) 
                    @{
                        var price = ((allInstallItem.Where(o => o.PoInstallId == m.PoInstallId).Sum(o => o.NetAmount) ?? 0).ToString("#,##0.00")); 
                    }
                    <i>
                        (<a class="blue" href="@Url.Action("Details", "PurchaseOrder", new{id=m.PoId})" target="_blank">@m.PurchaseOrder.Code</a> - <span style="color:orange">@price บาท</span>)
                    </i>
                </td>
                <td>
                    
                    @{
                        string installNumberText = "" + m.InstallNumber;
                        var maxInstallNumber = allInstall.Where(o => o.PoId == m.PoId).Max(o => o.InstallNumber);
                        if (maxInstallNumber != null)
                        {
                            installNumberText += "/" + maxInstallNumber;
                        }
                    }
                    <a href="@Url.Action("Details", "PurchaseOrder", new{id=m.PoId})" target="_blank">@installNumberText</a>
                </td>
                <td>
                  @Html.DisplayFor(model => m.PurchaseOrder.RequireDate)
                </td>
                <td>
                  @Html.DisplayFor(model => m.PurchaseOrder.MasPurchaseType.PcTypeName)
                </td>
                <td>
                  @Html.DisplayFor(model => m.PurchaseOrder.Bidder.MasContact.NameTh)
                </td>
                <td>
                  @*@Html.DisplayFor(model => m.PurchaseOrder.RecordStatus)*@
                    @{                        
                        var service = new KPIS.GERP.WEB.GIMS.RequestParcelService.RequestParcelServiceClient();
                        string status = @m.PurchaseOrder.RecordStatus;
                        string result = service.GetStatusName(status).ToString();
                        ViewBag.StatusName = result;
                    }
                    @*{          
                        var service2 = new KPIS.GERP.WEB.GIMS.DeliveryNoteService.DeliveryNoteServiceClient();
                        // var service = new KPIS.GERP.WEB.GIMS.RequestParcelService.RequestParcelServiceClient();

                        var Dn = service2.SelectDeliveryNote(new KPIS.GERP.Models.ICM.DeliveryNote(), null)
                            .Where(o => (o.RecordStatus == "U" || o.RecordStatus == "A") && o.RecordStatus != "D");
                 
                        //string status = @m.PurchaseOrder.RecordStatus;
                        var deliveryNote = Dn.FirstOrDefault(o => o.PoInstallId == m.PoInstallId);
                        string status = deliveryNote != null ? deliveryNote.RecordStatus : null;

                        string result;
                        switch (status)
                        {
                            case "U":
                                result = "ตรวจรับแล้ว";
                                break;
                            case "A":
                                result = "ตรวจรับแล้ว";
                                break;
                            case "W": 
                                result = "ยังไม่ส่งอนุมัติ";
                                break;
                            case "P":
                                result = "รออนุมัติ";
                                break;
                            case "C":
                                result = "ยกเลิก";
                                break;
                            case "J":
                                result = "ไม่ผ่านการอนุมัติ";
                                break;
                            default:
                                result = "ยังไม่ตรวจรับ";
                                break;
                        } *@
                    @ViewBag.StatusName
                </td>
               
                <td >

            
                
                @* @Html.ActionLink("ตรวจรับ", "Details", new { PoId=m.PoId, PoInstallId=m.PoInstallId}
                , new { @class="btn btn-small btn-success button-standard-width", role="button" })*@

          @Html.ActionLink("ตรวจรับ", "Create", new {PoId=m.PoId, PoInstallId=m.PoInstallId, nolayout = true}
                , new { @class="btn btn-small btn-success button-standard-width", role="button" })

								
									
                </td>
           </tr> 
        }
            
       
   </tbody>
       </table> 
              </div>

}
   

 @section scripts {
    
	<script type="text/javascript">

		$(function () {
			var oTable1 = $('#table').dataTable({
				"aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null,
                    null,
                    null,
                    { "bSortable": false }
				]
			});

			$('table th input:checkbox').on('click', function () {
				var that = this;
				$(this).closest('table').find('tr > td:first-child input:checkbox')
				.each(function () {
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});

			});

		    $('#bgyear').on('change', function() {
		        window.location = "@Url.Action("indexNotDelivery", "DeliveryNote")?bgyear=" + $('#bgyear').val();
		    });

			$('[data-rel=tooltip]').tooltip();

			$("form").submit(function () {
				var currentForm = this;
				bootbox.confirm(function (result) {
					var _result = result;
					if (result) {
						$.ajax({
						    url: "Details",
							type: "post",
							dataType: "json",
							data: $(currentForm).serialize(),
							success: function (result) {
								if (result.success) {
									location.reload();
								}
								else {
								}
							}
						});
					}
				})
				return false;

			});

		})

	</script>

}
        
